 /* 
 * File:   LateralAgent.cpp
 * Author: josep
 * 
 * Created on 17 / desembre / 2010, 12:29
 */

#include "cLateralAgent.h"

cLateralAgent::cLateralAgent() {
    inTheAir=true;
    //SetInTheAir(true);
    TimeToRelease=DEFAULT_TIME_TO_RELEASE;
    ObjetctType=AGENT_OBJECT_TYPE;
    dying=false;
}

cLateralAgent::cLateralAgent(const cLateralAgent& orig):cAnimatedVisibleObject(orig){
    dying=orig.dying;
    children = orig.children;
    FromLogicToViewCorrespondence = vector<int>(orig.FromLogicToViewCorrespondence);
}

cLateralAgent::~cLateralAgent() {
    delete sm;
}

void cLateralAgent::Stop(){
	sm->processTransition(STOP);
	SetSpeedX(0);
        //ChangeFramestate(STOP, -1, sm->getCurrentState()->getId());
}

void cLateralAgent::Jump(){
	sm->processTransition(JUMP);
	//SetSpeedY(UP);
        //ChangeFramestate(JUMP, -1, sm->getCurrentState()->getId());
}

void cLateralAgent::GoLeft(){
	sm->processTransition(TO_LEFT);
	//SetSpeedX(-GO);
        //ChangeFramestate(TO_LEFT, -1, sm->getCurrentState()->getId());
}

void cLateralAgent::GoRight(){
	sm->processTransition(TO_RIGHT);
	//SetSpeedX(GO);
        //ChangeFramestate(TO_RIGHT, -1, sm->getCurrentState()->getId());
}

int cLateralAgent::GetViewState(int logicState){
    return FromLogicToViewCorrespondence[logicState];
}

void cLateralAgent::SetInTheAir(bool inTheAir){
    //if(IsInTheAir() && !inTheAir){
    if(!inTheAir && ((sm->getCurrentState()->getId()==JUMPING_LEFT)
            || (sm->getCurrentState()->getId()==JUMPING_RIGHT))){
        sm->processTransition(STOP_JUMP);
    }
    cAnimatedVisibleObject::SetInTheAir(inTheAir);
}

/*Private code*/
int cLateralAgent::ChangeFramestate(int trans, int oldSt, int newSt){
    SetFrameState(GetViewState(newSt));
	return State<cLateralAgent>::CONTINUE;
}

void cLateralAgent::NextStep(int time){
    cAnimatedVisibleObject::NextStep(time);
    if(dying){
        dying=false;
        sm->processTransition(DIE);
    }
    if(isDead()){
        TimeToRelease--;
    }
}

void cLateralAgent::Init(cTexture* texture, int PosX, int PosY, int Width, int Height){
    cPhysicalVisibleObject::Init(texture, PosX, PosY, Width, Height);
    cLateralAgent::Init();
}

void cLateralAgent::Init(){
    FromLogicToViewCorrespondence.resize(1);
    FromLogicToViewCorrespondence[0]=0;
    //sm = new StateMachine<cLateralAgent>();
}

void cLateralAgent::Notify(cPhysicalReaction *revent){
    if(revent->GetReactionType()==cPhysicalReaction::AGENT_COLLISION){
        NotifyAgentCollision((cAgentCollisionEvent*) revent);
    }
}
bool cLateralAgent::NotifyAgentCollision(cAgentCollisionEvent *event){
    bool collides=true;
    //matar o morir
    if((GetSurfaceType(NORTH_SURFACE)<event->GetSurfaceType()[SOUTH_SURFACE])
                    &&(event->GetArea()->hasIncluded(event->GetReactionEvent(), c2DArea::INCLUDE_SOUTH))
                || (GetSurfaceType(SOUTH_SURFACE)<event->GetSurfaceType()[NORTH_SURFACE])
                    &&(event->GetArea()->hasIncluded(event->GetReactionEvent(), c2DArea::INCLUDE_NORTH))
                || (GetSurfaceType(EAST_SURFACE)<event->GetSurfaceType()[WEST_SURFACE])
                    &&(event->GetArea()->hasIncluded(event->GetReactionEvent(), c2DArea::INCLUDE_WEST))
                || (GetSurfaceType(WEST_SURFACE)<event->GetSurfaceType()[EAST_SURFACE])
                    &&(event->GetArea()->hasIncluded(event->GetReactionEvent(), c2DArea::INCLUDE_EAST))
                    ){
        //morir
        ToDie();
        collides=false;
    }
    return collides;
}

bool cLateralAgent::hasChild(){
    return !children.empty();
}

cPhysicalObject* cLateralAgent::popChild(){
    cPhysicalObject* ret = children.front();
    children.pop();
    return ret;
}

void cLateralAgent::ToDie(){
    dying=true;
}



